package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Permission;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (Permission)权限表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:02
 */
@RestController
@RequestMapping("permission")
@Api(value = "权限接口",description = "对权限表进行CRUD")
public class PermissionController {
    /**
     * 服务对象
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据权限id查询权限信息")
    @GetMapping("selectOne")
    public ResponseEntity selectOne(@ApiParam("权限id") Integer id) {
        Permission permission = this.permissionService.queryById(id);
        return new ResponseEntity(200,"查询单个权限信息成功",permission);
    }

    /**
     * 分页查询
     *
     * @param map 封装了分页信息和权限信息
     *
     * @return 分页信息
     */
    @ApiOperation("查询所有权限并分页:传入map")
    @PostMapping("selectAll")
    public ResponseEntity selectAll(@RequestBody Map<String,Object> map){
        int startPage = (int) map.get("startPage");
        int pageSize = (int) map.get("pageSize");
        JSONObject jsonObject=JSONObject.fromObject(map.get("permission"));
        Permission permission = (Permission)JSONObject.toBean(jsonObject, Permission.class);
        PageInfo<Permission> pageInfo = this.permissionService.queryAllPage(startPage,pageSize,permission);
        return new ResponseEntity(200,"分页查询权限信息成功",pageInfo);
    }

    /**
     * 新增一条权限信息
     *
     * @param permission 权限信息
     * @return 权限信息
     */

    @ApiOperation("新增一条权限信息:传入权限对象")
    @PostMapping("addPermission")
    public ResponseEntity addPermission(@RequestBody Permission permission){
        String result = this.permissionService.insert(permission);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过id删除权限信息
     *
     * @param id 权限id
     * @return  布尔值,是否成功
     */
    @ApiOperation("根据权限id删除权限信息")
    @RequestMapping("deletePermission")
    public ResponseEntity deletePermission(@ApiParam("权限id") int id){
        String result = this.permissionService.deleteById(id);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过传入的权限信息实体类修改信息,并返回修改后的权限信息实体
     *
     * @param permission 权限信息实体类
     * @return 权限信息实体类
     */
    @ApiOperation("修改权限信息:传入权限对象")
    @RequestMapping("updatePermission")
    public ResponseEntity updatePermission(@RequestBody Permission permission){
        String result = this.permissionService.update(permission);
        return new ResponseEntity(200, result, null);
    }

}
